$(function () {
//声明变量
    var member_dialog =  $("#member_dialog");
    var password_form =$('#password_form');
    var member_form = $("#member_form");
    var member_datagrid =   $("#member_datagrid");
    var min_datagrid =   $("#min_datagrid");
    var memberPassword_dialog =  $("#memberPassword_dialog");

    member_datagrid.datagrid({
        url:'/member/query.do',
        width:1350,
        height:350,
        fitColumns:true,
        toolbar:'#member_toolbar',
        pagination:true,
        rownumber:true,
        singleSelect:true,
        columns:[[
            {field:'id',checkbox:true},
            {field:'cardNumber',title:'会员卡号',width:80},
            {field:'name',title:'会员名称',width:80},
            {field:'tel',title:'电话',width:80},
            {field:'birthday',title:'会员生日',width:80},
            {field:'levelName',title:'会员等级',width:80},
            {field:'groupName',title:'会员分组',width:80},
            {field:'employeeName',title:'开卡员工',width:80},
            {field:'balance',title:'余额',width:80},
            {field:'address',title:'地址',width:80},
            {field:'points',title:'积分',width:80},
            {field:'sourceName',title:'客户来源',width:80}
        ]]
    });

    //统计top3
    min_datagrid.datagrid({
        url:'/member/orderByAmount.do',
        fit:true,
        fitColumns:true,
        rownumber:true,
        singleSelect:true,
        columns:[[
            {field:'id',checkbox:true},
            {field:'name',title:'会员姓名',width:80},
            {field:'saleAmmount',title:'消费总额',width:80}
            ]]
    });

    //配置dialog
    member_dialog.dialog({
        width:290,
        height:450,
        buttons:'#btn',
        close:true,
        onClose:function () {
            //关闭的同时清除表单数据
            member_form.form('clear');
        }
    });
    //修改密码的窗口
    memberPassword_dialog.dialog({
        width:350,
        height:230,
        buttons:'#passwordbtn',
        close:true,
        onClose:function () {
            //关闭的同时清除表单数据
            password_form.form('clear');
        }
    });
    //统一函数
    var methodObj = {
        add:function(){
        $("#psw").show();
        member_dialog.dialog('open');
        member_dialog.dialog('setTitle','新增会员');
        },

        edit:function (){
            //获取一行数据
            var row = member_datagrid.datagrid('getSelected');
            $("#psw").hide();
            if (!row) {
                //没有选择数据提示用户
                $.messager.alert("温馨提示","至少要选择一条数据","warning");
                return;//终止操作
            }
            member_form.form('load',row);
            //处理好回显数据后,打开编辑框
            member_dialog.dialog('open');
            //设置标题
            member_dialog.dialog('setTitle','编辑会员');
        },
        saved:function (){
            //提交表单
            member_form.form("submit",{
                url:'/member/saveOrUpdate.do',
                success:function (data) {
                    //回调函数:data表示返回数据解析为json
                    data = $.parseJSON(data);
                    if (data.success) {
                        //如果返回结果成功__弹出窗口提示
                        $.messager.alert('温馨提示','保存成功','info',function () {
                            methodObj.cancel();
                            member_datagrid.datagrid('reload');
                        })

                    }else {
                        //如果保存失败,则弹出窗口提示
                        $.messager.alert('温馨提示',data.msg,'error');
                    }
                }
            });
        },

        //删除
        remove:function() {
        var ele = member_datagrid.datagrid("getSelected");
        if (ele) {
            $.messager.confirm("温馨提示", "您确定要删除吗?", function (r) {
                if (r) {
                    $.get("/member/delete.do", {id: ele.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "删除成功", "info", function () {
                                member_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", "删除失败", "info")
                        }});}});
        }else{
            $.messager.alert("温馨提示", "请选择一行数据", "warning");
            return;
        }},

    savePassword:function (){
            //提交表单
            password_form.form("submit",{
                url:'/member/updatePassword.do',
                success:function (data) {
                    //回调函数:data表示返回数据解析为json
                    data = $.parseJSON(data);
                    if (data.success) {
                        //如果返回结果成功__弹出窗口提示
                        $.messager.alert('温馨提示','保存成功','info',function () {
                            methodObj.cancel();
                            member_datagrid.datagrid('reload')
                        })

                    }else {
                        //如果保存失败,则弹出窗口提示
                        $.messager.alert('温馨提示','保存失败','error');
                    }
                }

            });
        },
        cancel: function (){
            //关闭弹出框:
            member_dialog.dialog('close');
            memberPassword_dialog.dialog('close');

        },
        query:function (){
            //获取查询条件
            var keyword =  $("#keyword").textbox('getValue');
            //通过datagrid 的 load方法传递参数
            member_datagrid.datagrid('load',{
                keyword:keyword
            });
        },
        changePassword: function (){
            //获取一行数据
            var row = member_datagrid.datagrid('getSelected');
            //判断是否有选择选择数据
            if (!row) {
                //没有选择数据提示用户
                $.messager.alert("温馨提示","至少要选择一条数据","warning");
                return;//终止操作
            }
            password_form.form('load',row);
            memberPassword_dialog.dialog('open');
            //设置标题
            memberPassword_dialog.dialog('setTitle','修改密码');
        }
    };
    //统一绑定事件:
    $("a[data-cmd]").click(function () {
        var methodname = $(this).data("cmd");
        //动态获取调用方法名字,调用方法;
        methodObj[methodname]();
    });

    //发送ajax请求, 查询当天过生日的人数
    $.get("/member/selectBirthday.do",function (data) {
        $("#bircount").html(data);
    });
    //发送ajax请求, 查询当余额过生日的人数
    $.get("/member/selectBirthdayByMonth.do",function (data) {
        $("#month").html(data);
    });
    //发送ajax请求, 查询会员总数
    $.get("/member/selectCount.do",function (data) {
        $("#totalMember").html(data);
        var num=data/365;
        $("#average").html(num.toFixed(2));

    });
    //统计累计消费金额
    $.get("/product/money.do",function (data) {
        $("#totalExpense").html(data.totalAmount);
        $("#totalCount").html(data.totalNum);
        $("#averageExpense").html(data.prv);
    })
});

